const path = require('path')
const os = require('os')
const {BannerPlugin} = require('webpack')
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const {defineConfig} = require('@vue/cli-service')

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = defineConfig({
  transpileDependencies: ['*'],
  lintOnSave: false,
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
  productionSourceMap: false,
  // outputDir: 'dist',
  outputDir: path.resolve(__dirname, `dist`),
  devServer: {
    client: {
      overlay: false
    },
    port: 9155,
    open: false,
    proxy: {
      // 图片
      '/v1': {
        target: 'http://51.9.1.41:36998',
        ws: true,
        secure: false,
        changeOrigin: true,
        pathRewrite: {'^/v1': '/v1'}
      }
    }
  },
  chainWebpack: config => {
    config.resolve.alias.set('@', path.resolve(__dirname, `src`))
    config.resolve.alias.set('@lib', path.resolve(__dirname, `lib`))

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  },
  configureWebpack: (config) => {
    config.optimization.minimizer.push(
      // new UglifyJsPlugin({
      //   uglifyOptions: {
      //     compress: {
      //       drop_console: true, // console
      //       drop_debugger: false,
      //       pure_funcs: ['console.log'] // 移除console
      //     }
      //   }
      // })
    )
    if (process.env.NODE_ENV === 'production') {
      const rule = /app\.[\w]+\.js$/
      // 追加打包人信息-配置参考：https://doc.codingdict.com/webpack-cn-doc/plugins/banner-plugin/
      config.plugins.push(
        new BannerPlugin({
          raw: true,
          entryOnly: true,
          include: rule,
          banner: `console.log('build: ${os.hostname()} ${new Date().toLocaleString()}');`
        })
      )
    }
  },
  css: {
    loaderOptions: {
      scss: {
        additionalData: ``,
        sassOptions: {outputStyle: 'expanded'} // fix: 解决 element-ui 图标 icon 偶现乱码问题
      }
    }
  }
})
